package com.smasher.backup.dialog.progress

import android.content.res.ColorStateList
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.smasher.backup.dialog.BaseDialogFragment
import com.smasher.backup.dialog.R
import com.smasher.backup.dialog.databinding.FragmentDialogProgressBinding
import kotlinx.coroutines.launch

/**
 * @author HXZ_Android
 */
class ProgressDialogFragment : BaseDialogFragment() {

    private lateinit var mBinding: FragmentDialogProgressBinding

    private var backgroundDefault = R.drawable.dialog_background
    private var progressColorDefault = 0
    private var textColor = 0
    private var charSequence: CharSequence? = null


    override fun initView() {
        textColor = ContextCompat.getColor(requireContext(), R.color.color_CBF3D4)
        progressColorDefault = ContextCompat.getColor(requireContext(), R.color.color_CBF3D4)
        mBinding.progressText.text = charSequence
        mBinding.progressText.setTextColor(textColor)
        mBinding.progressBar.indeterminateTintList = buildColorStateList(progressColorDefault)
    }

    override fun initData() {}

    private fun buildColorStateList(color: Int): ColorStateList {
        val colors = intArrayOf(color)
        val states = arrayOfNulls<IntArray>(1)
        states[0] = intArrayOf()
        return ColorStateList(states, colors)
    }

    fun setBackgroundResource(@DrawableRes resId: Int) {
        backgroundDefault = resId
        dialog?.let {
            val window = it.window
            window?.setBackgroundDrawableResource(backgroundDefault)
        }
    }

    fun setTextColor(color: Int) {
        textColor = color
        lifecycleScope.launch {
            if (::mBinding.isInitialized) {
                mBinding.progressText.setTextColor(textColor)
            }
        }
    }

    fun setText(charSequence: CharSequence?) {
        this.charSequence = charSequence
        lifecycleScope.launch {
            if (::mBinding.isInitialized) {
                mBinding.progressText.setText(charSequence)
            }
        }
    }

    fun setProgressTintColor(color: Int) {
        progressColorDefault = color
        lifecycleScope.launch {
            if (::mBinding.isInitialized) {
                mBinding.progressBar.indeterminateTintList =
                    buildColorStateList(progressColorDefault)
            }
        }
    }

    fun updateText(charSequence: CharSequence?) {
        this.charSequence = charSequence
        lifecycleScope.launch {
            if (::mBinding.isInitialized) {
                mBinding.progressText.text = charSequence
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //R.style.CustomDialog
        setStyle(STYLE_NO_TITLE, 0)
        arguments?.let {
            charSequence = it.getString(KEY_MESSAGE, "")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentDialogProgressBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onStart() {
        super.onStart()
        if (dialog != null) {
            dialog!!.setCanceledOnTouchOutside(false)
            // 设置宽度为屏宽、靠近屏幕底部。
            val window = dialog!!.window
            if (window != null) {
                window.setBackgroundDrawableResource(backgroundDefault)
                window.decorView.setPadding(0, 0, 0, 0)
                val wlp = window.attributes
                wlp.gravity = Gravity.CENTER
                wlp.width = WindowManager.LayoutParams.WRAP_CONTENT
                wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
                window.attributes = wlp
            }
        }
    }

    companion object {
        const val TAG = "PROGRESS"

        private const val KEY_MESSAGE = "message"

        @JvmStatic
        fun newInstance(message: String): ProgressDialogFragment {
            return ProgressDialogFragment().apply {
                arguments = Bundle().apply {
                    putString(KEY_MESSAGE, message)
                }
            }
        }
    }
}